clear all
set more off, permanent
set maxvar 20000

**************************
cd "ADD FILEPATH HERE"
local initDataDownload 1
**************************

	if `initDataDownload' == 1 {
		cd data
		copy https://www.federalreserve.gov/econres/files/scf2016s.zip ./, replace
		unzipfile scf2016s.zip, replace
		cd ..
	}
	
	use data/p16i6, clear
	rename *, upper

******************************************************************************
*** Data Cleaning: Following FRB SAS Macro Summary Variables When Possible ***
******************************************************************************
	do "scf2016_dataclean"

**********************
*** Data Filtering ***
**********************

*** Drop observations based on chosen conditions

	* Head aged 25-61
	keep if AGE >= 25 & AGE < 62
	quietly summ atincome [fw = wgt], detail
	scalar sample_size_0 = r(sum_w)

	* Owns home
	keep if HOUSECL == 1
	quietly summ atincome [fw = wgt], detail
	scalar sample_size_1 = r(sum_w)
	
	* Household in the labor force
	keep if lfp == 1

	* Has a credit card
	drop if hasVisa != 1

	* After-tax permanent income between 1st and 99th percentile
	quietly summ atincome [fw = wgt], detail
	scalar income_p1 = r(p1)
	scalar income_p99 = r(p99)

	drop if atincome < income_p1
	drop if atincome > income_p99

	* Home-income ratio below 95th percentile
	quietly summ HOUSESscaled [fw = wgt], detail
	scalar h_p95 = r(p95)
	drop if HOUSESscaled > h_p95
	
	* Get final sample size
	quietly summ atincome [fw = wgt], detail
	scalar sample_size_2 = r(sum_w)

**********************
*** SCF Statistics ***
**********************
	putexcel set "SCF_results_16.xlsx", modify

	* Home Value to Permanent Income
	summ HOUSESscaled [fw = wgt]
		putexcel A2="Mean"
		putexcel B1="Home Val / Income"
		putexcel B2=`r(mean)'

	* Loan-to-Value Ratio
	summ ltv [fw = wgt]
		putexcel C1="LTV"
		putexcel C2=`r(mean)'

	* Share of Households with Positive Revolving Credit Card Debt
	summ hccdebt [fw = wgt]
		putexcel D1="Positive CC Debt Share"
		putexcel D2=`r(mean)'

	* Share of Households with Positive Revolving Credit Card Debt (High Interest)
	summ hccdebt_highint [fw = wgt]
		putexcel E1="Positive CC Debt Share (High Interest)"
		putexcel E2=`r(mean)'

	* Credit Card Debt to Permanent Income (High Interest)
	summ ccdebt_highintscaled [fw = wgt]
		putexcel F1="CC Debt / Income (High Interest)"
		putexcel F2=`r(mean)'

	* Credit Card Borrowing Limit to Permanent Income
	summ credlimitscaled [fw = wgt]
		putexcel G1="Cred. Limit / Income"
		putexcel G2=`r(mean)'

	*  After-Tax Permanent Income
	summ atincome [fw = wgt]
		putexcel H1="After-Tax Income"
		putexcel H2=`r(mean)'

	*  Share Close to Credit Limit
	quietly summ credlimitscaled [fw = wgt], detail
	scalar c_p5 = r(p5)
	summ closetoconstraint [fw = wgt] if credlimitscaled>c_p5
		putexcel I1="Share Close to Constraint"
		putexcel I2=`r(mean)'
		
	*  Sample Sizes
		putexcel J1="Sample Sizes"
		putexcel J2=sample_size_0
		putexcel J3=sample_size_1
		putexcel J4=sample_size_2



